package com.example.waimai004.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.waimai004.entity.Employee;
import com.example.waimai004.entity.Grate;
import com.example.waimai004.service.GrateService;
import com.java.demo.common.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/grate")
public class GrateController {
    @Autowired
    GrateService grateService;

//    @PostMapping("getList")
//    public List<Employee> getgrate(){
//        LambdaQueryWrapper<Grate> wrapper=new LambdaQueryWrapper<>();
//        wrapper.gt(Grate::getJava,70).and()
//                .li
//
//    }

//    查询
    @GetMapping("getList2")
    public  R<List<Grate>> getlist2(){
        LambdaQueryWrapper<Grate> wrapper=new LambdaQueryWrapper<>();
        wrapper.gt(Grate::getPython,80).or()
                .gt(Grate::getJava,80).orderByDesc(Grate::getId);
        List<Grate> list=grateService.list(wrapper);
        return R.success(list);
    }


//    新增
    @GetMapping("addGrade")
    public R<String> addGrade(@RequestBody Grate grate){
        boolean save=grateService.save(grate);
        if (save){
            return R.success("添加成功");
        }
        return R.error("发送失败");
    }
//    修改
    @PutMapping("updateGrade")
    public R<String> updateGrate(@RequestBody int id){
        LambdaUpdateWrapper<Grate> wrapper= new LambdaUpdateWrapper<>();
        wrapper.set(Grate::getJava,100)
                .set(Grate::getPython,95)
                .eq(Grate::getId,5);
        Boolean update=grateService.update(wrapper);
        if (update)
            return R.success("修改成功");
        return R.error("修改失败");
    }
//    分页查询
    @GetMapping("/getpage/{current}/{size}")
    public  R<Page<Grate>> getpage(@RequestBody int current,
                                   @RequestBody int size){
      Page<Grate> page=new Page<>(current,size);
      LambdaQueryWrapper<Grate> wrapper=new LambdaQueryWrapper<>();
      wrapper.orderByDesc(Grate::getId);
      Page<Grate> page1=grateService.page(page,wrapper);
      return R.success(page1);
    }
}
